package br.com.spring.mvc.dto;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import br.com.spring.mvc.entity.infra.Message;
import br.com.spring.mvc.entity.infra.TypeMessage;

public abstract class BaseDTO {

	private List<Message> messages = new ArrayList<Message>();
		
	public void addMessage(Message message){
		messages.add(message);		
	}
	
	public void addSuccesMessage(String message){
		messages.add(Message.getSuccesMessage(message));		
	}
	
	@SuppressWarnings("rawtypes")
	public void addError(List<ObjectError> erros) throws Exception{
		for (Iterator iterator = erros.iterator(); iterator.hasNext();) {
			ObjectError objectError = (ObjectError) iterator.next();
			Message message = new Message(TypeMessage.ERROR, objectError.getDefaultMessage());
			messages.add(message);
		}
		if(messages.size() > 0){throw new Exception();}
	}
	
	public abstract void validate(BindingResult result) throws Exception;
}
